package com.rw.coco.handler.message.reply.handler.validate;

import com.rw.coco.handler.message.event.CocoGroupMessageEvent;
import com.rw.coco.handler.message.reply.GroupReplyHandler;
import com.rw.coco.util.CocoCommandUtil;
import com.rw.coco.util.CocoReplyUtil;
import com.rw.tool.util.string.StringUtil;

/**
 * 群组开关处理器
 */
public class GroupSwitchHandler extends GroupReplyHandler {
    @Override
    protected boolean groupTrigger(CocoGroupMessageEvent groupEvent) {
        String botOn = CocoCommandUtil.COMMAND_PROP.get(CocoCommandUtil.BOT_ON);
        String botOff = CocoCommandUtil.COMMAND_PROP.get(CocoCommandUtil.BOT_OFF);

        // 如果指令匹配，则拦截
        return StringUtil.equalsIgnoreCase(groupEvent.getMessage(), botOn) ||
                StringUtil.equalsIgnoreCase(groupEvent.getMessage(), botOff);
    }

    @Override
    protected void groupHandler(CocoGroupMessageEvent groupEvent) {
        String botOn = CocoCommandUtil.COMMAND_PROP.get(CocoCommandUtil.BOT_ON);
        String botOff = CocoCommandUtil.COMMAND_PROP.get(CocoCommandUtil.BOT_OFF);
        if (StringUtil.equalsIgnoreCase(groupEvent.getMessage(), botOn)) {
            // 如果不是超级管理员、群聊管理或群主，则直接回复权限不足
            if (!groupEvent.isAdmin() && !groupEvent.getAccountModel().isSuperAdmin()) {
                String reply = CocoReplyUtil.REPLY_PROP.get(CocoReplyUtil.BOT_ON_WITHOUT_AUTHORIZE);
                groupEvent.sendBack(reply);
                return;
            }

            // 回复
            String reply = CocoReplyUtil.REPLY_PROP.get(CocoReplyUtil.BOT_ON);
            groupEvent.sendBack(reply);

            // 开启群组开关
            groupEvent.getPersistenceMapper().groupSwitchOn(groupEvent.getGroup().getId());
        } else if (StringUtil.equalsIgnoreCase(groupEvent.getMessage(), botOff)) {
            // 如果不是超级管理员、群聊管理或群主，则直接回复权限不足
            if (!groupEvent.isAdmin() && !groupEvent.getAccountModel().isSuperAdmin()) {
                String reply = CocoReplyUtil.REPLY_PROP.get(CocoReplyUtil.BOT_OFF_WITHOUT_AUTHORIZE);
                groupEvent.sendBack(reply);
                return;
            }

            // 回复
            String reply = CocoReplyUtil.REPLY_PROP.get(CocoReplyUtil.BOT_OFF);
            groupEvent.sendBack(reply);

            // 关闭群组开关
            groupEvent.getPersistenceMapper().groupSwitchOff(groupEvent.getGroup().getId());
        }
    }
}
